


function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return r[2];
    };
    return 0;
 }


/*
$(document).ready(function() {
  // 在这里编写需要执行的 JavaScript 代码


    $.ajax({
        url: "https://www.gongrenbao.cn/index.php?c=api/ozon/get1688",
      dataType: 'jsonp',
      success: function(data) {

         console.log(data)
      },
      error: function(xhr, textStatus, errorThrown) {
        console.log('JSONP 请求发生错误：' + xhr.statusText);
      }

    });


   var data = [];

   setTimeout(() => {
     $("body").find(".offer-attr-item").each(function(e){
         var dataItem = {};
        $(this).find("span").each(function(e){
            console.log($(this).html())
            if($(this).attr("class")=="offer-attr-item-name"){
                dataItem.key = $(this).html();
            }else if($(this).attr("class")=="offer-attr-item-value"){
                dataItem.value = $(this).html();
                 data.push(dataItem);
            }
        });
        console.log(data);
    })
   },2000);
});*/


// 监听页面加载完成事件，并在加载完成后弹出浮层
window.addEventListener('load', function() {
    setTimeout(() => {
	showPopup();
	},2000);
});

// 获取数据并更新页面内容
function fetchData(inner) {
    var dataPro = [];
    $("body").find(".offer-attr-item").each(function(e){
         var dataItem = {};
        $(this).find("span").each(function(e){
            console.log($(this).html())
            if($(this).attr("class")=="offer-attr-item-name"){
                dataItem.key = $(this).html();
            }else if($(this).attr("class")=="offer-attr-item-value"){
                dataItem.value = $(this).html();
                dataPro.push(dataItem);
            }
        });
    })
    console.log(dataPro);

    var title = $(".title-text").html();
    var pis = [];

    $("body").find(".detail-gallery-img").each(function(e){
        pis.push($(this).attr("src"));
    })
    console.log(pis);
    /*var skuKey = [];
    $(".next-table-header").find("th").each(function(e){
        skuKey.push($(this).find("span").attr("title"))
    })
    console.log(skuKey);
    var url = window.location.search;
    console.log(url);

    var skuValue= [];
    $(".next-table-body").find("tr").each(function(e){
        var skuItem= [];
        $(this).find("td").each(function(e){
            skuItem.push( $(this).find("span").attr("title"));
        })
        skuValue.push($skuItem);
    })
    console.log(skuValue);*/
    var jsData;
    $("script").each(function(e){
        var scriptData = $(this).html();
        if (scriptData.indexOf('window.__INIT_DATA=') !== -1) {
            const parts = scriptData.split('window.__INIT_DATA=');
            jsData = parts[1];
        }
    })

    const url = new URL(location.href);
    const pathname = url.pathname;
    const parts = pathname.split('/');
    const spu_id = parts[2].split('.')[0];

    var offerLineCheck = $(".mod-detail-offline-title").html();
    var dataOffLine = {};
    if(offerLineCheck=='商品已下架'){
        dataOffLine.spu_id = spu_id;
    }
    var onlyPrice = {};
    const price = $('meta[property="og:product:price"]').attr('content');
    if(price){
        onlyPrice.price =price;
        onlyPrice.spu_id =spu_id;
    }

    var detailContent = $(".content-detail").html();
    $.post("https://www.gongrenbao.cn/index.php?c=api/ozon/1688pub",
          {data:jsData,dataPro:dataPro,dataOffLine:dataOffLine,onlyPrice:onlyPrice,detailContent:detailContent},
          function(data){
            console.log(data);
          },
      "text");
}

function showPopup() {

  var auto_get = getQueryString("auto_get");
  console.log("auto_get_"+auto_get);
  if(auto_get=="true"){
        fetchData();
        return;

  }
  // 创建浮层最外层容器
  var container = document.createElement('div');
  container.id = 'popup-container';

  // 创建浮层内部容器
  var inner = document.createElement('div');
  inner.id = 'popup';
  inner.innerHTML = '<h3>这是一个弹出浮层</h3><p>您可以在这里显示您想要的内容</p>';

  // 创建关闭按钮
  var closeBtn = document.createElement('button');
  closeBtn.innerHTML = '关闭';
  closeBtn.onclick = function() {
    container.parentNode.removeChild(container);
  };

  // 创建刷新按钮
  var refreshBtn = document.createElement('button');
  refreshBtn.innerHTML = '采集数据';
  refreshBtn.onclick = function() {
    fetchData(inner);
  };

  // 把内部容器和按钮添加到最外层容器中
  container.appendChild(inner);
  container.appendChild(closeBtn);
  container.appendChild(refreshBtn);

  // 把最外层容器添加到页面中
  document.body.appendChild(container);

  // 设置浮层样式
  var popupContainer = document.getElementById('popup-container');
  var popup = document.getElementById('popup');
  popupContainer.style.position = 'fixed';
  popupContainer.style.left = '0';
  popupContainer.style.top = '50%';
  popupContainer.style.transform = 'translateY(-50%)';
  popupContainer.style.zIndex = '9999';
  popup.style.backgroundColor = 'lightblue';
  popup.style.padding = '10px';
  popup.style.borderRadius = '10px';
  popup.style.boxShadow = '2px 2px 2px rgba(0,0,0,0.4)';
  closeBtn.style.marginRight = '10px';
  closeBtn.style.backgroundColor = '#ff9797';
  closeBtn.style.border = 'none';
  closeBtn.style.color = '#fff';
  closeBtn.style.padding = '5px 10px';
  closeBtn.style.borderRadius = '5px';
  closeBtn.style.cursor = 'pointer';
  refreshBtn.style.backgroundColor = '#7cd6ff';
  refreshBtn.style.border = 'none';
  refreshBtn.style.color = '#fff';
  refreshBtn.style.padding = '5px 10px';
  refreshBtn.style.borderRadius = '5px';
  refreshBtn.style.cursor = 'pointer';
}
